/****************************************************************************
 * Chariott - Continuous Integration Testing Tool
 * Zaid Mohsin
 * The University of York
 * 2012/2013
 * MSc in Information Technology
 * All rights reserved.
 ****************************************************************************/
package chariott.configuration;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.ini4j.Ini;
import org.ini4j.Profile.Section;

import chariott.partition.AlgorithmType;

public class ConfigResolver {

	private Ini ini;

	/**
	 * class to parse the configurations file and expose it to the whole system.
	 * 
	 * @param configFile
	 *            The configuration file to parse
	 * @throws IOException
	 *             throws exception if no file was found
	 */
	public ConfigResolver(File configFile) throws IOException {

		// create the instance from the config file "chariottconfig.ini"
		ini = new Ini();

		ini.load(new FileReader(configFile));

		parseGlobals();
	}

	private void parseGlobals() {

		Section AWS = ini.get("AWS");
		Section AWSInstance = ini.get("AWSInstance");
		Section SSH = ini.get("SSH");
		Section Testing = ini.get("Testing");
		Section SVN = ini.get("SVN");

		// parse AWSInstance section
		Globals.AWS_AMI = AWSInstance.fetch("AMI");
		Globals.AWS_ENDPOINT = AWS.fetch("endPoint");
		Globals.AWS_INSTANCE_TYPE = AWSInstance.fetch("instanceType");
		Globals.AWS_KEY_NAME = AWSInstance.fetch("keyName");
		Globals.AWS_SECURITY_GROUP = AWSInstance.fetch("securityGroup");
		Globals.AWS_NO_OF_INSTANCES = AWSInstance.fetch("numOfInstances");

		// parse SSH section
		Globals.SSH_PEM_FILE = SSH.fetch("pemFile");
		Globals.SSH_SESSION_USER = SSH.fetch("sessionUser");

		// parse Testing config section
		Globals.TESTING_CONFIG_FILE = Testing.fetch("testConfigFile");
		Globals.TESTING_PROJECT_NAME = Testing.fetch("projectName");
		Globals.TESTING_TEST_FOLDER = Testing.fetch("testFolder");

		// set the type of algorithm selected
		String myAlgorithm = Testing.fetch("algorithmType");
		if (myAlgorithm.equals("ROUNDROBIN"))
			Globals.ALGORITHM_TYPE = AlgorithmType.ROUNDROBIN;
		else if (myAlgorithm.equals("LEASTUSED"))
			Globals.ALGORITHM_TYPE = AlgorithmType.LEASTUSED;
		else if (myAlgorithm.equals("BP_FF"))
			Globals.ALGORITHM_TYPE = AlgorithmType.BINPACKING_FIRSTFIT;
		else if (myAlgorithm.equals("BP_FF_DEC"))
			Globals.ALGORITHM_TYPE = AlgorithmType.BINPACKING_FF_DECREASING;
		else if (myAlgorithm.equals("AUTO"))
			Globals.ALGORITHM_TYPE = AlgorithmType.AUTO;
		else {
			System.out
					.println("Please specifiy a sorting algorithm or use 'AUTO' for automatic selection");
			System.exit(0);
		}

		// parse bin size if binpacking algorithms were set
		if (Globals.ALGORITHM_TYPE == AlgorithmType.BINPACKING_FIRSTFIT
				|| Globals.ALGORITHM_TYPE == AlgorithmType.BINPACKING_FF_DECREASING)
			Globals.BIN_SIZE = Testing.fetch("binSize");
		else
			Globals.BIN_SIZE = "0";

		// parse SVN config section
		Globals.SVN_REPOSITORY = SVN.fetch("repository");

		// set the system properties to include the AWS credentials
		System.setProperty("aws.accessKeyId", AWS.fetch("accessKey"));
		System.setProperty("aws.secretKey", AWS.fetch("secretKey"));

	}

}
